package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.repository;

import cn.hutool.core.util.StrUtil;
import com.iwhalecloud.citybrain.flow.platform.manager.application.utils.BeanCopierUtils;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.ModelFlowObjectProcessInstance;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.ModelFlowObjectProcessInstanceDomain;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowObjectProcessInstanceRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowObjectProcessInstance;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowObjectProcessInstanceExample;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.mapper.FlowObjectProcessInstanceDAO;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Repository
public class FlowObjectProcessInstanceRepositoryImpl implements FlowObjectProcessInstanceRepository {

    @Resource
    private FlowObjectProcessInstanceDAO flowObjectProcessInstanceDAO;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return flowObjectProcessInstanceDAO.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(ModelFlowObjectProcessInstance record) {
        FlowObjectProcessInstance entity = new FlowObjectProcessInstance();
        BeanCopierUtils.copyProperties(record,entity);
        return flowObjectProcessInstanceDAO.insert(entity);
    }

    @Override
    public int insertSelective(ModelFlowObjectProcessInstance record) {
        FlowObjectProcessInstance entity = new FlowObjectProcessInstance();
        BeanCopierUtils.copyProperties(record,entity);
        return flowObjectProcessInstanceDAO.insertSelective(entity);
    }

    @Override
    public ModelFlowObjectProcessInstance selectByPrimaryKey(Long id) {
        FlowObjectProcessInstance record = flowObjectProcessInstanceDAO.selectByPrimaryKey(id);
        ModelFlowObjectProcessInstance entity = new ModelFlowObjectProcessInstance();
        BeanCopierUtils.copyProperties(record,entity);
        return entity;
    }

    @Override
    public int updateByPrimaryKeySelective(ModelFlowObjectProcessInstance record) {
        FlowObjectProcessInstance entity = new FlowObjectProcessInstance();
        BeanCopierUtils.copyProperties(record,entity);
        return flowObjectProcessInstanceDAO.updateByPrimaryKeySelective(entity);
    }

    @Override
    public int updateByPrimaryKey(ModelFlowObjectProcessInstance record) {
        FlowObjectProcessInstance entity = new FlowObjectProcessInstance();
        BeanCopierUtils.copyProperties(record,entity);
        return flowObjectProcessInstanceDAO.updateByPrimaryKey(entity);
    }

    @Override
    public List<ModelFlowObjectProcessInstanceDomain> getFlowObjectProcessInstanceDomailList(String objectInstancdId, String processInstanceId, String taskNodeCode) {
        FlowObjectProcessInstanceExample query = new FlowObjectProcessInstanceExample();
        FlowObjectProcessInstanceExample.Criteria criteria = query.createCriteria();
        if (StrUtil.isNotEmpty(processInstanceId)) {
            criteria.andProcessInstanceIdEqualTo(processInstanceId);
        }
        if (StrUtil.isNotEmpty(objectInstancdId)) {
            criteria.andObjectInstanceIdEqualTo(objectInstancdId);
        }
        if (StrUtil.isNotEmpty(taskNodeCode)) {
            criteria.andTaskNodeCodeEqualTo(taskNodeCode);
        }
        query.setOrderByClause("id desc");
        List<FlowObjectProcessInstance> configs = flowObjectProcessInstanceDAO.selectByExampleWithCriteria(query);
        return configs.stream().map(this::convertToDoamain).collect(Collectors.toList());
    }

    @Override
    public void updateFlowObjectProcessInstance(ModelFlowObjectProcessInstanceDomain domain) {
        FlowObjectProcessInstance entity = new FlowObjectProcessInstance();
        BeanCopierUtils.copyProperties(domain,entity);
        entity.setUpdateTime(new Date());
        flowObjectProcessInstanceDAO.updateByPrimaryKeySelective(entity);
    }

    private ModelFlowObjectProcessInstanceDomain convertToDoamain(FlowObjectProcessInstance record){
        if (Objects.isNull(record)) {
            return null;
        }
        ModelFlowObjectProcessInstanceDomain config = new ModelFlowObjectProcessInstanceDomain();
        BeanCopierUtils.copyProperties(record, config);
        return config;
    }
}
